package com.taptech.doufu.ui.view.choice;

import android.content.Context;
import android.content.Intent;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.ui.activity.EasyBrowseActivity;
import com.taptech.doufu.ui.activity.MoreNovel2Activity;
import com.taptech.doufu.ui.activity.NewAlbumsActivity;
import com.taptech.doufu.ui.activity.NovelCopyRightActivity;
import com.taptech.doufu.ui.activity.NovelDetailsActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.CoverType;
import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.bean.choice.BannerModel;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.util.GlideRoundTransform;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.StartActivityUtils;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.util.TagsUtil;

import java.util.HashMap;

public class ChoiceUtils {

    public static int getPicWidth(Context context) {
        return getLayWidth(context) - ScreenUtil.dip2px(context, 12);
    }

    public static int getLayWidth(Context context) {
        return (ScreenUtil.getScreenWidth(context)-ScreenUtil.dip2px(context,12))/4;
    }

    public static int getHeight(Context context) {
        return (int) (1.33 * getPicWidth(context));
    }

    public static void displayImage(Context context, String url, ImageView imageView) {
        GlideUtil.displayRoundImage(context,imageView,url,3,R.drawable.img_default_loading_l);
    }

    public static void clickBanner(Context context, BannerModel bean, String title) {
        if (bean == null) {
            return;
        }
        Intent intent = new Intent();
        switch (bean.getBanner_type()){
            case Constant.MainHomeImageCardType.JUMP_TYPE_OUTSIDE_WEBVIEW:
                intent.setClass(context, EasyBrowseActivity.class);
                intent.putExtra(Constant.URL, bean.getBanner_link());
                intent.putExtra(Constant.TITLE, title);
                context.startActivity(intent);
                break;
            case Constant.MainHomeImageCardType.JUMP_TYPE_INSIDE_WEBVIEW:
                intent.setClass(context, BrowseActivity.class);
                intent.putExtra(Constant.URL, bean.getBanner_link());
                intent.putExtra(Constant.TITLE, title);
                context.startActivity(intent);
                break;
            case Constant.MainHomeImageCardType.JUMP_ALBUM:
                intent.setClass(context, NewAlbumsActivity.class);
                intent.putExtra(Constant.URL, bean.getBanner_link());
                intent.putExtra(Constant.NAME, title);
                context.startActivity(intent);
                break;
            case Constant.MainHomeImageCardType.JUMP_TYPE_WEEX:
                SimpleWeexActivity.startActivity(context,bean.getBanner_link());
                break;
            case Constant.MainHomeImageCardType.JUMP_IP:
                NovelCopyRightActivity.Companion.startActivity(context);
                break;
            default:
                SimpleWeexActivity.startActivity(context,bean.getBanner_link());
                break;
        }
    }

    public static void jumpToNovelDetail(Context context,int novelId) {

        Intent intent = new Intent();
        intent.putExtra(Constant.ARTICLEID, String.valueOf(novelId));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("id", novelId);
        intent.setClass(context, NovelDetailsActivity.class);
        context.startActivity(intent);
    }

    public static void jumpTagDetail(Context context,String tagName) {
        TagsUtil.openWeexTagActivity(context,tagName,Constant.OBJECT_TYPE_NOVEL,"");

    }

    public static void jumpToMoreComic(Context context, String title, String moreUrl) {
        if(moreUrl!=null && moreUrl.contains("/weex/")){
            SimpleWeexActivity.startActivity(context,moreUrl);
        }else{
            CartoonServices.clickMore(context,moreUrl,title,CoverType.COVER_TYPE_VERTICAL);
        }
    }

    public static void jumpToMoreNovel(Context context, String title, String moreUrl) {
        if(moreUrl!=null && moreUrl.contains("/weex/")){
            SimpleWeexActivity.startActivity(context,moreUrl);
        }else{
            Intent intent = new Intent();
            intent.putExtra(Constant.URL, moreUrl);
            intent.putExtra("IS_FROMHOME", true);
            intent.putExtra(Constant.TITLE, title);
            new StartActivityUtils(context, intent).toNovelMoreActivity();
        }
    }

    /**
     * 统计精选页面卡片数据
     * @param context
     * @param eventName
     * @param eventBean
     */
    public static void addEvent(Context context, String eventName, EventBean eventBean) {
        HashMap<String, String> map = new HashMap<>();
        map.put("position", String.valueOf(eventBean.getPosition()));
        map.put("cardType", String.valueOf(eventBean.getCardType()));
        map.put("moreType", String.valueOf(eventBean.getMoreType()));
        map.put("title", eventBean.getTitle());
        TMAnalysis.event(context, eventName,map);
    }

}
